<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');
$f = isset($_REQUEST['f'])?$_REQUEST['f']:'index';
if(!in_array($f,array('index','edit','olist','detail','delete','status','cancel'))){
	exit('Access Denied!');
}
if(isset($_G['usergroup']['access']["ec$f"]) && !ican("ec$f")){
	msg('没有权限');
}
$m = new OrderModule;
$m->$f();
class OrderModule
{
	function index(){
		global $_G;
		
		include admintemplate('order');
	}
	function olist(){
		global $_G;
		$wheresql = '';
		$lang = lang::load('point');
		$page = max(1, getGP('page','G','int'));
		$pagesize = 10;
		$type=getGP('t','G');
		$offset = ($page - 1) * $pagesize;
		$url = 'admin.php?m=order&f=olist';
		$status=array('0'=>'等待确认','1'=>'已经确认','2'=>'正在配货', '4'=>'正在配送','5'=>'用户已收货', '7'=>'交易结束','9'=>'交易取消','10'=>'换货');
		
		$stxnid = getGP('stxnid','G','int');
		$smail = getGP('smail','G');
		if ($stxnid) {
			$wheresql .= " AND o.txnid = '$stxnid'";
			$url .= '&stxnid='.$stxnid;
		}else{
			$stxnid='';
		}
		if ($_GET['orderstatus']!='') {
			$orderstatus = getGP('orderstatus','G','int');
			$wheresql .= " AND o.status = '$orderstatus'";
			$url .= '&orderstatus='.$orderstatus;
		}
		if ($type) {
			$wheresql .= " AND o.type = '$type'";
			$url .= '&t='.$t;
		}
		if ($smail) {
			$wheresql .= " AND o.mail = '$smail'";
			$url .= '&orderstatus='.rawurlencode($smail);
		}
		
		$num = $_G['db']->result("SELECT COUNT(*) AS num FROM".table('orders')."o WHERE 1 $wheresql");
		$result = $_G['db']->fetch_all("SELECT o.*,u.username,s.username as seller FROM".table('orders')."o LEFT JOIN".table('users')."u ON o.uid = u.uid LEFT JOIN".table('users')."s ON o.touid = s.uid WHERE 1 $wheresql ORDER BY o.txnid DESC LIMIT $offset, $pagesize");
		
		include admintemplate('order');
	}
	function detail(){
		global $_G;
		$id = getGP('id','G','int');
		if(!$id){
			msg('');
		}
		$lang = lang::load('user');
		$lang = lang::load('point');
		//$rs = $_G['db']->fetch_one_array("SELECT o.*,u.username,s.username as seller FROM".table('orders')."o LEFT JOIN".table('users')."u ON o.uid = u.uid LEFT JOIN".table('users')."s ON o.touid = s.uid WHERE o.txnid=$id ");
		$data = $_G['db']->fetch_one_array("SELECT o.*,u.username,s.username as seller,i.* FROM".table('orders')."o LEFT JOIN".table('users')."u ON o.uid = u.uid LEFT JOIN".table('users')."s ON o.touid = s.uid  LEFT JOIN".table('orders_shipping')."i ON o.txnid = i.txnid WHERE o.txnid=$id ");
		$goods=$_G['db']->fetch_all("SELECT * FROM".table('orders_product')." WHERE txnid=$id");
		if($data['type']=='pointpay'){
					if($_G['settings']['point']['pointratio'])$ratio = 1/$_G['settings']['point']['pointratio'];
					$setting = unserialize(decrypt($_G['settings']['point']['pointsetting']));
				}
		$logs=	$_G['db']->fetch_all("SELECT * FROM".table('orders_log')." WHERE txnid=$id");	
		
		
		include admintemplate('order');
	}
	function edit(){
		global $_G;
		$id = getGP('id','G','int');
		if(!$id){
			msg('');
		}
		$lang = lang::load('user');
		$lang = lang::load('point');
		//$rs = $_G['db']->fetch_one_array("SELECT o.*,u.username,s.username as seller FROM".table('orders')."o LEFT JOIN".table('users')."u ON o.uid = u.uid LEFT JOIN".table('users')."s ON o.touid = s.uid WHERE o.txnid=$id ");
		$data = $_G['db']->fetch_one_array("SELECT o.*,u.username,s.username as seller,i.* FROM".table('orders')."o LEFT JOIN".table('users')."u ON o.uid = u.uid LEFT JOIN".table('users')."s ON o.touid = s.uid  LEFT JOIN".table('orders_shipping')."i ON o.txnid = i.txnid WHERE o.txnid=$id ");
		$goods=$_G['db']->fetch_all("SELECT * FROM".table('orders_product')." WHERE txnid=$id");
			if($data['type']=='pointpay'){
					if($_G['settings']['point']['pointratio'])$ratio = 1/$_G['settings']['point']['pointratio'];
					$setting = unserialize(decrypt($_G['settings']['point']['pointsetting']));
			}
		m('addr');
		$region=AddrModule::getaddr($data['rid']);	
		include admintemplate('order_edit');
	}
	
	function delete(){
		global $_G;
		$ids=getGP('id','P','array');
		if(!empty($ids)){
			if(!ISADMIN){
				$where=" AND touid=".$_G['uid'];
			}
			$_G['db']->query("DELETE FROM".table('orders')." WHERE txnid IN(".implode(',', $ids).")".$where);
		}
		msg("操作成功");
	}
	function status(){
		global $_G;
		$id=getGP('id','G','int');
		$id = getGP('id','G','int');
		$return['s'] = false;
		$return['m'] = '';
		if(!$id){
			json($return);
		}
		$orders = $_G['db']->fetch('orders','txnid,status,payment_date,payment_type' ,array('txnid'=>$id));
		if(!$orders['txnid']){
			json($return);
		}
		switch($orders['status']){
			case '0':
				$_G['db']->update('orders',array('status'=>1),array('txnid'=>$id));
				$return['s'] = true;
				$return['m'] = '操作成功';
				$_G['db']->insert("orders_log",array('txnid'=>$id,'uid'=>$_G['uid'],'username'=>$_G['username'],'data'=>'确认订单', 'dateline'=>TIMESTAMP));
				json($return);
				break;
			case '1':
				$_G['db']->update('orders',array('status'=>2),array('txnid'=>$id));
				$return['s'] = true;
				$return['m'] = '操作成功';
				$_G['db']->insert("orders_log",array('txnid'=>$id,'uid'=>$_G['uid'],'username'=>$_G['username'],'data'=>'配货', 'dateline'=>TIMESTAMP));
				json($return);
				break;
			case '2':
				$_G['db']->update('orders',array('status'=>4),array('txnid'=>$id));
				$return['s'] = true;
				$return['m'] = '操作成功';
				$_G['db']->insert("orders_log",array('txnid'=>$id,'uid'=>$_G['uid'],'username'=>$_G['username'],'data'=>'配送', 'dateline'=>TIMESTAMP));
				json($return);
				break;	
			case '4':
			case '5':
				$data['status']=7;
				$data['duedate']=TIMESTAMP;
				if($orders['payment_date']<1){
					$data['payment_date']=TIMESTAMP;
				}
				$_G['db']->update('orders',$data,array('txnid'=>$id));
				$return['s'] = true;
				$return['m'] = '操作成功';
				$_G['db']->insert("orders_log",array('txnid'=>$id,'uid'=>$_G['uid'],'username'=>$_G['username'],'data'=>'结束', 'dateline'=>TIMESTAMP));
				json($return);
				break;	
		}
	}
	
	function cancel(){
		global $_G;
		$id = getGP('id','G','int');
		$return['s'] = false;
		$return['m'] = '';
		if(!$id){
			json($return);
		}
		$orders = $_G['db']->fetch('orders','txnid,status' ,array('txnid'=>$id));
		if(!$orders['txnid']){
			json($return);
		}
		$_G['db']->update('orders',array('status'=>9),array('txnid'=>$id,'status'=>'<7'));
		$return['s'] = true;
		$return['m'] = '操作成功';
		$_G['db']->insert("orders_log",array('txnid'=>$id,'uid'=>$_G['uid'],'username'=>$_G['username'],'data'=>'取消订单', 'dateline'=>TIMESTAMP));
				
		json($return);
	}
}